package org.jradweb.core.taglib;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyContent;

import org.springframework.beans.BeansException;

public class JsWindowTag extends UriTag {
	private String wid;
	public String getWid() {
		return wid;
	}

	public void setWid(String wid) {
		this.wid = wid;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	private String title="";
	
	@Override
	public int doEndTag() throws JspException {
		HttpServletRequest req=(HttpServletRequest)pageContext.getRequest();
		String contextPath=req.getContextPath();
		JspWriter writer =this.pageContext.getOut();
		String uri=this.getUri(contextPath);
		 
		
		try {
			writer.write("j.ajax({ type: 'GET', url: '");
			writer.write(uri);
			writer.write("', success: function(msg){j.window({");
			if(this.wid!=null){
				writer.write("id:'");
				writer.write(wid);
				writer.write("',");
			}
			writer.write("content:msg,title:'");
			writer.write(title);
			writer.write("'}).show()}}); ");
			
		   
			
			return EVAL_PAGE;
		} catch (BeansException e) {
			throw new JspException(e);
		} catch (IOException e) {
			throw new JspException(e);
		}
		finally{
			pageContext.removeAttribute(URI_PARAMETERS);
			pageContext.removeAttribute(URI_QURY_PARAMETERS);
		}
	}

}
